package com.dongdong.controller;

/**
 * @author dongdong 抖音号：W13883155245
 * @version 1.0
 * @date 2025/5/5
 * @description
 */

import com.dongdong.controller.utils.R;
import com.dongdong.service.FileService;
import io.minio.errors.MinioException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@RestController
@RequestMapping("/file")
@CrossOrigin(origins = "*")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     *  uploadFormData.append('file', selectedFile.value);
     *   uploadFormData.append('userId', formData.value.id);
     *   uploadFormData.append('pileNumber', formData.value.pileNumber);
     *   uploadFormData.append('operation', formData.value.operation);
     * @param file
     * @param userId
     * @return
     */
    @PostMapping("/upload")
    public R uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("userId") Long userId, @RequestParam("pileNumber") String pileNumber,@RequestParam("operation") String operation) {
        System.out.println(file.getOriginalFilename());
        System.out.println(userId+"--"+pileNumber+"--"+operation);
        try {
            //fileService.uploadFile(file, userId);
            fileService.uploadFile(file, userId,pileNumber,operation);
            return new R("文件上传成功");
        } catch (Exception e) {
            return new R("文件上传失败 " + e.getMessage());
        }
    }

      @GetMapping("/download")
    public R downloadFiles(HttpServletRequest request,HttpServletResponse response, @RequestParam("userId") Long userId) {
        try {
            fileService.downloadFiles(request,response, userId);
            return new R("下载完成");
        } catch (Exception e) {
            // 可以根据实际情况处理异常，比如返回错误信息给前端
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/pileImage")
    public R findPileImage(@RequestParam("pileNumber") String pileNumber) {
          fileService.getPileImagePath(pileNumber);
         return new R(fileService.getPileImagePath(pileNumber));
    }

    @DeleteMapping("/deleteImage")
    public R deleteImage(  @RequestParam("fileId") Long fileId,@RequestParam("userId") Long userId) {
        try {

            return new R( fileService.deleteFile(fileId,userId));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

@DeleteMapping("/deleteInformation")
    public R deleteInformation(  @RequestParam("pileNumber") String pileNumber,@RequestParam("userId") Long userId) {
        try {
            fileService.deleteFileByPileNumber(pileNumber,userId);
            return new R("数据已经删除 ");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}